<?php namespace App\Services;

use App\Facades\Infra\MessageManage;
use App\Facades\Infra\WeChatManage;
use App\Utils\SendMessage;

class MessageService{
	public function getMessages(){

		$conditions['wechat_doctor_info_id'] = $this->getDoctorId();

		return MessageManage::getMessages($conditions)->get();
	}

	private function getDoctorId(){
		$openid = session('wechat.oauth_user')->getId();

		$conditions['openid'] = $openid;

		$wechatUser = WeChatManage::getUserInfo($conditions);

		return $wechatUser->wechatDoctorInfo->id;
	}

	public function createReply($data){
		$data['reply_type'] = 1;

		MessageManage::createReply($data);

		$conditions['id'] = $data['message_id'];

		$toOpenId = MessageManage::getUser($conditions)->wechatUser->openid;

		$message = "来自于医生的回复：".$data['reply_content'];

		$this->sendMessageToWeChat($toOpenId,$message);

		return;
	}

	private function sendMessageToWeChat($toOpenId,$message){
		SendMessage::send($toOpenId,$message);
	}
}